﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using System.Windows;
using System.Collections.ObjectModel;


namespace QuanLyDuocPham.Controllers.QuanLyHeThong.DanhMuc
{
    public class NHANVIEN_Control : IDisposable
    {

        DuocPhamDataContext db = new DuocPhamDataContext(staticClass.Conn);


        public void Dispose()
        {
            if (db != null)
            {
                db.Dispose();
                db = null;
            }
        }
        //-------------------------------------------------------


        public void loadComboBoxU(ComboBox cmb)
        {

            cmb.Items.Clear();
            var types = db.NHANVIEN_Get_List();
            var typeorder = types.OrderBy(w => w.HO_TEN);

            foreach (var t in typeorder)
            {
                if (t.TEN_DN == null)

                    cmb.Items.Add(t);
            }
            cmb.SelectedValuePath = "MA_NHANVIEN";
            cmb.IsTextSearchEnabled = true;
            cmb.IsTextSearchCaseSensitive = false;
            cmb.IsEditable = true;
            NHANVIEN_Get_ListResult temp = new NHANVIEN_Get_ListResult();

            temp.MA_NHANVIEN = "...";
            temp.HO_TEN = "Thêm mới";
            cmb.Items.Add(temp);
        }
        
        public void loadComboBox(ComboBox cmb)
        {

            cmb.Items.Clear();
            var types = db.NHANVIEN_Get_List();

            var typeorder = types.OrderBy(w => w.HO_TEN);

            foreach (var t in typeorder)
            {
                cmb.Items.Add(t);
            }
            
            cmb.SelectedValuePath = "MA_NHANVIEN";
            
            cmb.IsTextSearchEnabled = true;
            cmb.IsTextSearchCaseSensitive = false;
            cmb.IsEditable = true;
            NHANVIEN_Get_ListResult temp = new NHANVIEN_Get_ListResult();

            temp.MA_NHANVIEN = "...";
            temp.HO_TEN = "Thêm mới";
            cmb.Items.Add(temp);
        }
        public void addData(TextBox txtMa_NV, ComboBox cmbKho, ComboBox cmbNhaThuoc, TextBox txtHo_Ten, CheckBox chbGioi_Tinh, TextBox txtDia_Chi, TextBox txtD_Thoai, TextBox txtD_Dong, TextBox txtEmail, TextBox txtTrinh_Do, CheckBox chbTrangThai, TextBox txtGhi_Chu)
        {
            if (validateData(txtHo_Ten, txtDia_Chi))
            {
                DuocPhamDataContext db = new DuocPhamDataContext();
                
                var Kho = (KHO_Get_ListResult)cmbKho.SelectedItem;
                var NhaThuoc = (NHATHUOC_Get_ListResult)cmbNhaThuoc.SelectedItem;

                db.NHANVIEN_Insert(
                    txtMa_NV.Text,null, Kho.MA_KHO, NhaThuoc.MA_NHATHUOC, null, null,
                    txtHo_Ten.Text, chbGioi_Tinh.IsChecked,                    
                    txtDia_Chi.Text,
                    txtD_Thoai.Text,
                    txtD_Dong.Text, txtEmail.Text, txtTrinh_Do.Text,
                    chbTrangThai.IsChecked,
                    txtGhi_Chu.Text
                );

                System.Windows.Forms.MessageBox.Show("Đã thêm thành công!");
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("Dữ liệu không hợp lệ!");
            }
        }

        private bool validateData(TextBox txtHo_Ten, TextBox txtDia_Chi)
        {
            if (txtHo_Ten.Text.Trim() == "" || txtDia_Chi.Text.Trim() == "")
                return false;
            else

            return true;
        }

        public void updateData(TextBox txtMa_NV, ComboBox cmbKho, ComboBox cmbNhaThuoc, TextBox txtHo_Ten, CheckBox chbGioi_Tinh, TextBox txtDia_Chi, TextBox txtD_Thoai, TextBox txtD_Dong, TextBox txtEmail, TextBox txtTrinh_Do, CheckBox chbTrangThai, TextBox txtGhi_Chu)
        {
            if (validateData(txtHo_Ten,txtDia_Chi) == false)
                MessageBox.Show("Chưa nhập đủ thông tin!");
            else
            {
                var Kho = (KHO_Get_ListResult)cmbKho.SelectedItem;
                var NhaThuoc = (NHATHUOC_Get_ListResult)cmbNhaThuoc.SelectedItem;

                db.NHANVIEN_Update(txtMa_NV.Text,
                    txtMa_NV.Text, null, Kho.MA_KHO, NhaThuoc.MA_NHATHUOC, null, null,
                    txtHo_Ten.Text, chbGioi_Tinh.IsChecked,
                    txtDia_Chi.Text,
                    txtD_Thoai.Text,
                    txtD_Dong.Text, txtEmail.Text, txtTrinh_Do.Text,
                    chbTrangThai.IsChecked,
                    txtGhi_Chu.Text
                );

                MessageBox.Show("Đã cập nhật!");
            }
        }


        public void loadData(ListView List)
        {
            List.Items.Clear();
            var list = db.NHANVIEN_Get_List();
            var typeorder = list.OrderBy(w => w.HO_TEN);

            foreach (var t in typeorder)
            {
                List.Items.Add(t);
            }
        }

        public void loadData(DataPager dp)
        {
            
            var list = db.NHANVIEN_Get_List();
            var typeorder = list.OrderBy(w => w.HO_TEN);
            ObservableCollection<Object> dpSource = new ObservableCollection<Object>();
            int i = 1;
            foreach (var t in typeorder)
            {
                if (t.HO_TEN.Trim() != "" && t.TRANG_THAI == true)
                {
                    dpSource.Add(new { STT = i, t.GIOI_TINH, t.DI_DONG, t.DIA_CHI, t.DIEN_THOAI, t.EMAIL, t.MA_KHO, t.MA_NHATHUOC, t.TRINH_DO, t.GHI_CHU, t.MA_NHANVIEN, t.TRANG_THAI });
                    i++;
                }
            }
            dp.ItemsSource = dpSource;
            dp.ItemsPerPage = 10;
        }
        public string loadID_moi(){
            string ID = "";

            int rowcount = db.NHANVIEN_Get_List().Count();
            if (rowcount == 0)
            {
                ID = "NV00000001";
            }
            else
            {
                for (int i = 1; i < rowcount+2; i++)
                {
                    string IDtam = "NV";
                    for (int j = 0; j < (8 - i.ToString().Length); j++)
                        IDtam = IDtam + "0";
                    IDtam = IDtam + i;
                    Boolean kq = true;
                    var data = db.NHANVIEN_Get_List();

                    foreach (var t in data)
                    {

                        if (t.MA_NHANVIEN.Trim() == IDtam.Trim())

                            kq = false;
                    }
                    if (kq == true)
                    {
                        ID = IDtam;
                        break;
                    }
                }

            }
            return ID;

        }

        KHO_Control ctrl1 = new KHO_Control();
        NHATHUOC_Control ctrl2 = new NHATHUOC_Control();
        public void loadForm_Edit(string ID_Edit, TextBox txtMa_NV, ComboBox cmbKho, ComboBox cmbNhaThuoc, TextBox txtHo_Ten, CheckBox chbGioi_Tinh, TextBox txtDia_Chi, TextBox txtD_Thoai, TextBox txtD_Dong, TextBox txtEmail, TextBox txtTrinh_Do, CheckBox chbTrangThai, TextBox txtGhi_Chu)
        {

            var sl = (NHANVIEN_GetByMaNhanvienResult)db.NHANVIEN_GetByMaNhanvien(ID_Edit).Single();


            txtMa_NV.Text = sl.MA_NHANVIEN;

            ctrl1.loadComboBox(cmbKho);
            int t = 0;
            foreach (var i in cmbKho.Items)
            {
                var temp = (KHO_Get_ListResult)i;
                if (temp.MA_KHO== sl.MA_KHO)
                {
                    break;
                }
                t++;
            }
            cmbKho.SelectedIndex = t;
            ctrl2.loadComboBox(cmbNhaThuoc);
            t = 0;
            foreach (var i in cmbNhaThuoc.Items)
            {
                var temp = (NHATHUOC_Get_ListResult)i;
                if (temp.MA_NHATHUOC == sl.MA_NHATHUOC)
                {
                    break;
                }
                t++;
            }
            cmbNhaThuoc.SelectedIndex = t;

            txtHo_Ten.Text = sl.HO_TEN;
            txtEmail.Text = sl.EMAIL;
            txtTrinh_Do.Text = sl.TRINH_DO;
            txtDia_Chi.Text = sl.DIA_CHI;
            txtD_Thoai.Text = sl.DIEN_THOAI;
            txtD_Dong.Text = sl.DI_DONG;
            txtGhi_Chu.Text = sl.GHI_CHU;
            chbTrangThai.IsChecked = sl.TRANG_THAI;
            chbGioi_Tinh.IsChecked = sl.GIOI_TINH;
            
        }


        public string deleteData(string ID_Delete, string error)
        {
            error = "";
            if (db.PHIEUNHAP_GetByMaNhanvien(ID_Delete).Count() > 0 || db.PHIEUXUAT_GetByMaNhanvien(ID_Delete).Count() > 0 || db.LS_DANGNHAP_GetByMaNhanvien(ID_Delete).Count() > 0 || db.CA_CT_GetByMaNhanvien(ID_Delete).Count() > 0 || db.THUCHI_GetByMaNhanvien(ID_Delete).Count() > 0)
                error = "Không thể xóa";
            else
            {
                db.NHANVIEN_Delete(ID_Delete);
                error = "Xóa thành công!";
            }
            return error;

        }
    }
}
